/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat
{
    /// <summary>
    /// 超链接视图行为
    /// </summary>
    public enum FCLinkBehavior
    {
        /// <summary>
        /// 总是显示下划线
        /// </summary>
        AlwaysUnderLine,
        /// <summary>
        /// 触摸悬停时显示下划线
        /// </summary>
        HoverUnderLine,
        /// <summary>
        /// 总是不显示下划线
        /// </summary>
        NeverUnderLine
    }

    /// <summary>
    /// 超链接视图
    /// </summary>
    public partial class FCLinkLabel : FCLabel
    {
        /// <summary>
        /// 创建超链接视图
        /// </summary>
        public FCLinkLabel()
        {
            setCursor(FCCursors.Hand);
        }

        /// <summary>
        /// 是否已访问
        /// </summary>
        public bool m_visited = false;

        public long m_activeLinkColor = FCColor.rgb(255, 0, 0);

        /// <summary>
        /// 获取单击超链接时的颜色
        /// </summary>
        public virtual long getActiveLinkColor()
        {
            return m_activeLinkColor;
        }

        /// <summary>
        /// 设置单击超链接时的颜色
        /// </summary>
        public virtual void setActiveLinkColor(long value)
        {
            m_activeLinkColor = value;
        }

        public long m_disabledLinkColor = FCColor.rgb(133, 133, 133);

        /// <summary>
        /// 获取超链接被禁用时的颜色
        /// </summary>
        public virtual long getDisabledLinkColor()
        {
            return m_disabledLinkColor;
        }

        /// <summary>
        /// 设置超链接被禁用时的颜色
        /// </summary>
        public virtual void setDisabledLinkColor(long value)
        {
            m_disabledLinkColor = value;
        }

        public FCLinkBehavior m_linkBehavior = FCLinkBehavior.AlwaysUnderLine;

        /// <summary>
        /// 获取下划线的显示方式
        /// </summary>
        public virtual FCLinkBehavior getLinkBehavior()
        {
            return m_linkBehavior;
        }

        /// <summary>
        /// 设置下划线的显示方式
        /// </summary>
        public virtual void setLinkBehavior(FCLinkBehavior value)
        {
            m_linkBehavior = value;
        }

        public long m_linkColor = FCColor.rgb(0, 0, 255);

        /// <summary>
        /// 获取超链接处于默认时的颜色
        /// </summary>
        public virtual long getLinkColor()
        {
            return m_linkColor;
        }

        /// <summary>
        /// 设置超链接处于默认时的颜色
        /// </summary>
        public virtual void setLinkColor(long value)
        {
            m_linkColor = value;
        }

        public bool m_linkVisited = false;

        /// <summary>
        /// 获取是否按照已访问的样式显示超链接颜色
        /// </summary>
        public virtual bool isLinkVisited()
        {
            return m_linkVisited;
        }

        /// <summary>
        /// 设置是否按照已访问的样式显示超链接颜色
        /// </summary>
        public virtual void setLinkVisited(bool value)
        {
            m_linkVisited = value;
        }

        public long m_visitedLinkColor = FCColor.rgb(128, 0, 128);

        /// <summary>
        /// 获取已访问的超链接的颜色
        /// </summary>
        public virtual long getVisitedLinkColor()
        {
            return m_visitedLinkColor;
        }

        /// <summary>
        /// 设置已访问的超链接的颜色
        /// </summary>
        public virtual void setVisitedLinkColor(long value)
        {
            m_visitedLinkColor = value;
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType()
        {
            return "LinkLabel";
        }

        /// <summary>
        /// 获取要绘制的超链接颜色
        /// </summary>
        /// <returns>前景色</returns>
        public virtual long getPaintingLinkColor()
        {
            if (isEnabled())
            {
                FCNative native = getNative();
                if (this == native.getHoveredView())
                {
                    return m_activeLinkColor;
                }
                else if (this == native.getPushedView())
                {
                    return m_activeLinkColor;
                }
                else
                {
                    if (m_linkVisited && m_visited)
                    {
                        return m_visitedLinkColor;
                    }
                    else
                    {
                        return m_linkColor;
                    }
                }
            }
            else
            {
                return m_disabledLinkColor;
            }
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type)
        {
            switch (name)
            {
                case "activelinkcolor":
                    type = "color";
                    value = FCTran.colorToStr(getActiveLinkColor());
                    break;
                case "disabledlinkcolor":
                    type = "color";
                    value = FCTran.colorToStr(getDisabledLinkColor());
                    break;
                case "linkbehavior":
                    {
                        type = "enum:FCLinkBehavior";
                        FCLinkBehavior linkBehavior = getLinkBehavior();
                        if (linkBehavior == FCLinkBehavior.AlwaysUnderLine)
                        {
                            value = "AlwaysUnderLine";
                        }
                        else if (linkBehavior == FCLinkBehavior.HoverUnderLine)
                        {
                            value = "HoverUnderLine";
                        }
                        else
                        {
                            value = "NeverUnderLine";
                        }
                        break;
                    }
                case "linkcolor":
                    type = "color";
                    value = FCTran.colorToStr(getLinkColor());
                    break;
                case "linkvisited":
                    type = "bool";
                    value = FCTran.boolToStr(isLinkVisited());
                    break;
                case "visitedlinkcolor":
                    type = "color";
                    value = FCTran.colorToStr(getVisitedLinkColor());
                    break;
                default:
                    base.getAttribute(name, ref value, ref type);
                    break;
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames()
        {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "ActiveLinkColor", "DisabledLinkColor", "LinkBehavior", "LinkColor", "LinkVisited", "VisitedLinkColor" });
            return attributeNames;
        }

        /// <summary>
        /// 触摸点击方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onClick(FCTouchInfo touchInfo)
        {
            base.onClick(touchInfo);
            if (m_linkVisited)
            {
                m_visited = true;
            }
        }

        /// <summary>
        /// 触摸按下方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchDown(FCTouchInfo touchInfo)
        {
            base.onTouchDown(touchInfo);
            invalidate();
        }

        /// <summary>
        /// 触摸进入方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchEnter(FCTouchInfo touchInfo)
        {
            base.onTouchEnter(touchInfo);
            invalidate();
        }

        /// <summary>
        /// 触摸离开方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchLeave(FCTouchInfo touchInfo)
        {
            base.onTouchLeave(touchInfo);
            invalidate();
        }

        /// <summary>
        /// 触摸抬起方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchUp(FCTouchInfo touchInfo)
        {
            base.onTouchUp(touchInfo);
            invalidate();
        }

        /// <summary>
        /// 重绘前景方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintForeground(FCPaint paint, FCRect clipRect)
        {
            String text = getText();
            //绘制文字
            if (text != null && text.Length > 0)
            {
                int width = getWidth(), height = getHeight();
                if (width > 0 && height > 0)
                {
                    FCFont font = getFont();
                    FCSize tSize = paint.textSize(text, font, -1);
                    long linkColor = getPaintingLinkColor();
                    FCPoint tPoint = new FCPoint((width - tSize.cx) / 2, (height - tSize.cy) / 2);
                    FCPadding padding = getPadding(); ;
                    switch (m_textAlign)
                    {
                        case FCContentAlignment.BottomCenter:
                            tPoint.y = height - tSize.cy;
                            break;
                        case FCContentAlignment.BottomLeft:
                            tPoint.x = padding.left;
                            tPoint.y = height - tSize.cy - padding.bottom;
                            break;
                        case FCContentAlignment.BottomRight:
                            tPoint.x = width - tSize.cx - padding.right;
                            tPoint.y = height - tSize.cy - padding.bottom;
                            break;
                        case FCContentAlignment.MiddleLeft:
                            tPoint.x = padding.left;
                            break;
                        case FCContentAlignment.MiddleRight:
                            tPoint.x = width - tSize.cx - padding.right;
                            break;
                        case FCContentAlignment.TopCenter:
                            tPoint.y = padding.top;
                            break;
                        case FCContentAlignment.TopLeft:
                            tPoint.x = padding.left;
                            tPoint.y = padding.top;
                            break;
                        case FCContentAlignment.TopRight:
                            tPoint.x = width - tSize.cx - padding.right;
                            tPoint.y = padding.top;
                            break;
                    }
                    FCRect tRect = new FCRect(tPoint.x, tPoint.y, tPoint.x + tSize.cx, tPoint.y + tSize.cy);
                    if (autoEllipsis() && (tRect.right > clipRect.right || tRect.bottom > clipRect.bottom))
                    {
                        if (tRect.right > clipRect.right)
                        {
                            tRect.right = clipRect.right;
                        }
                        if (tRect.bottom > clipRect.bottom)
                        {
                            tRect.bottom = clipRect.bottom;
                        }
                        paint.drawTextAutoEllipsis(text, linkColor, font, tRect);
                    }
                    else
                    {
                        paint.drawText(text, linkColor, font, tRect, -1);
                    }
                    //画下划线
                    FCNative native = getNative();
                    if (m_linkBehavior == FCLinkBehavior.AlwaysUnderLine || (m_linkBehavior == FCLinkBehavior.HoverUnderLine && (this == native.getPushedView() || this == native.getHoveredView())))
                    {
                        paint.drawLine(linkColor, 1, 0, tPoint.x, tPoint.y + tSize.cy, tPoint.x + tSize.cx, tPoint.y + tSize.cy);
                    }
                }
            }
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value)
        {
            switch (name)
            {
                case "activelinkcolor":
                    setActiveLinkColor(FCTran.strToColor(value));
                    break;
                case "disabledlinkcolor":
                    setDisabledLinkColor(FCTran.strToColor(value));
                    break;
                case "linkbehavior":
                    {
                        value = value.ToLower();
                        if (value == "alwaysunderline")
                        {
                            setLinkBehavior(FCLinkBehavior.AlwaysUnderLine);
                        }
                        else if (value == "hoverunderline")
                        {
                            setLinkBehavior(FCLinkBehavior.HoverUnderLine);
                        }
                        else
                        {
                            setLinkBehavior(FCLinkBehavior.NeverUnderLine);
                        }
                        break;
                    }
                case "linkcolor":
                    setLinkColor(FCTran.strToColor(value));
                    break;
                case "linkvisited":
                    setLinkVisited(FCTran.strToBool(value));
                    break;
                case "visitedlinkcolor":
                    setVisitedLinkColor(FCTran.strToColor(value));
                    break;
                default:
                    base.setAttribute(name, value);
                    break;
            }
        }
    }
}
